package com.yeyks.controller.admin;

import com.google.common.collect.Lists;
import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.annotation.AdminAppLog;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.appcommon.dto.AppStarLevelDTO;
import com.yeyks.common.appcommon.dto.account.AppAccountDTO;
import com.yeyks.common.appcommon.dto.hotel.AppHotelBasicDTO;
import com.yeyks.common.appcommon.dto.hotel.AppHotelDetailDTO;
import com.yeyks.common.appcommon.dto.hotel.AppHotelInfoDTO;
import com.yeyks.common.appcommon.dto.hotel.AppHotelTypeDTO;
import com.yeyks.common.appcommon.dto.room.AppRoomNoteDTO;
import com.yeyks.common.appcommon.param.hotel.*;
import com.yeyks.common.appcommon.param.room.SetRoomNoteParam;
import com.yeyks.common.appcommon.result.CommonPage;
import com.yeyks.common.appcommon.result.CommonResult;
import com.yeyks.common.dto.mq.EsUpdateMessage;
import com.yeyks.common.exception.OftenFiledException;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.utils.apputil.bean.MyBeanUtils;
import com.yeyks.commonReference.append.rocketmq.service.producer.MQProducerService;
import com.yeyks.commonReference.service.account.AccountInfoService;
import com.yeyks.commonReference.service.hotel.HotelInfoService;
import com.yeyks.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

import static com.yeyks.common.constants.SysLogMsg.*;

/**
 * 酒店管理接口
 * @author Administrator
 */
@Api(tags = "AppHotelController_酒店管理接口", description = "酒店管理相关接口[列表,酒店信息,公司信息,公司资质,账户信息,入住须知]")
@RestController
@RequestMapping("/admin/hotel")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AppHotelController extends BaseController {

    private final HotelInfoService hotelInfoService;
    /**
     * 禁用，启用，发送MQ，修改ES的最新数据
     */
    private final MQProducerService producerService;

    private final AccountInfoService accountInfoService;


    /**
     * 分页查询酒店信息
     */
    @ApiOperation("分页查询酒店信息（支持只查询某一种酒店）")
    @PostMapping("/list")
    public CommonResult<CommonPage<AppHotelBasicDTO>> list(@Valid QueryHotelParam queryHotelParam){


        AppAccountDTO appAccountDTO = validToken(queryHotelParam.getToken(), queryHotelParam.getVersion(), queryHotelParam.getPlatform());

        return hotelInfoService.listPage(queryHotelParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
        //return appHotelService.list(queryHotelParam);
    }

    /**
     * 分页查询优享VIP酒店
     */
    @ApiOperation("分页查询优享VIP酒店（1.7.0）")
    @PostMapping("/list/min/vip")
    public CommonResult<CommonPage<AppHotelBasicDTO>> minVipList(@Valid QueryHotelParam queryHotelParam){


        AppAccountDTO appAccountDTO = validToken(queryHotelParam.getToken(), queryHotelParam.getVersion(), queryHotelParam.getPlatform());

        queryHotelParam.setVipFlag(3);
        return hotelInfoService.listPage(queryHotelParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
        //return appHotelService.list(queryHotelParam);
    }

    @AdminAppLog(value = HOTEL_UPTO_JIP)
    @ApiOperation("升级优享VIP酒店（1.7.0）")
    @PostMapping("/upgrade/min/vip")
    public CommonResult upgradeMinVipHotel(@Valid UpdateUpgradeJipHotelParam param){

        validToken(param.getToken(), param.getVersion(), param.getPlatform());
        //2， 优享开发商
        List<AccountInfo> accountInfos = accountInfoService.findDeveloper(param.getName(), 2);
        if(CollectionUtils.isEmpty(accountInfos)){
            return CommonResult.failed("开发商输入名称有误");
        }
        try {
            AjaxResponseBody body = hotelInfoService.upgradeJipHotel(param.getHotelId(), accountInfos.get(0).getId());
            if(body.getCode() == 0){
                return CommonResult.success("升级优享酒店成功");
            }
        }catch (ServiceException e){
            throw new OftenFiledException(e.getMessage());
        }
        return CommonResult.failed("升级优享酒店失败");
    }

    /**
     * 根据账号Id 分页查询酒店信息[查询的签约酒店]
     */
    @ApiOperation("根据账号Id 分页查询酒店信息[查询的签约酒店]")
    @PostMapping("/query/list/accountId")
    public CommonResult<CommonPage<AppHotelBasicDTO>> queryListByAccountId(@Valid QueryHotelByAccountIdParam queryHotelByAccountIdParam){

        AppAccountDTO appAccountDTO = validToken(queryHotelByAccountIdParam.getToken(),
                queryHotelByAccountIdParam.getVersion(), queryHotelByAccountIdParam.getPlatform());

        return hotelInfoService.queryListByAccountId(queryHotelByAccountIdParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
        //return appHotelService.queryListByAccountId(queryHotelByAccountIdParam);
    }


    @ApiOperation("获取酒店的相关详情信息")
    @PostMapping("/get/detail")
    public CommonResult<AppHotelDetailDTO> getDetail(@Valid GetHotelDetailParam getHotelDetailParam){

        AppAccountDTO appAccountDTO = validToken(getHotelDetailParam.getToken(),
                getHotelDetailParam.getVersion(), getHotelDetailParam.getPlatform());

        return hotelInfoService.getHotelDetail(getHotelDetailParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }

    @AdminAppLog(value = HOTEL_UPDATE_MSG)
    @ApiOperation("更新酒店信息")
    @PostMapping("/update")
    public CommonResult updateHotel(@Valid UpdateHotelInfoParam hotelInfoParam){

        AppAccountDTO appAccountDTO = validToken(hotelInfoParam.getToken(),
                hotelInfoParam.getVersion(), hotelInfoParam.getPlatform());

        return hotelInfoService.updateHotel(hotelInfoParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }

    @AdminAppLog(value = HOTEL_UPDATE_USE)
    @ApiOperation("禁用启用酒店接口")
    @PostMapping("/valid")
    public CommonResult validHotel(@Valid UpdateValidHotelParam updateValidHotelParam){

        AppAccountDTO appAccountDTO = validToken(updateValidHotelParam.getToken(),
                updateValidHotelParam.getVersion(), updateValidHotelParam.getPlatform());

        CommonResult commonResult = hotelInfoService.validHotel(updateValidHotelParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class),
                null);
        if(commonResult.getCode() == CommonResult.SUCCESS){
            List<String> sourceIds = Lists.newArrayList();
            sourceIds.add(String.valueOf(updateValidHotelParam.getId()));
            producerService.sendEsUpdate(new EsUpdateMessage().setType("hotel")
                    .setSourceType("yyks").setSourceIds(sourceIds),0);
        }
        return commonResult;

    }

    @AdminAppLog(value = HOTEL_ENABLE_USE)
    @ApiOperation("启用酒店接口_V1.6.1")
    @PostMapping("/enable")
    public CommonResult enableHotel(@Valid UpdateValidHotelParam updateValidHotelParam){

        AppAccountDTO appAccountDTO = validToken(updateValidHotelParam.getToken(),
                updateValidHotelParam.getVersion(), updateValidHotelParam.getPlatform());
        CommonResult commonResult = hotelInfoService.validHotel(updateValidHotelParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class),
                true);
        if(commonResult.getCode() == CommonResult.SUCCESS){
            List<String> sourceIds = Lists.newArrayList();
            sourceIds.add(String.valueOf(updateValidHotelParam.getId()));
            //更新酒店状态，发送Mq更新ES
            producerService.sendEsUpdate(new EsUpdateMessage().setType("hotel")
                    .setSourceType("yyks").setSourceIds(sourceIds),0);
        }
        return commonResult;
    }

    @AdminAppLog(value = HOTEL_DISABLE_USE)
    @ApiOperation("禁用酒店接口_V1.6.1")
    @PostMapping("/disable")
    public CommonResult disableHotel(@Valid UpdateValidHotelParam updateValidHotelParam){

        AppAccountDTO appAccountDTO = validToken(updateValidHotelParam.getToken(),
                updateValidHotelParam.getVersion(), updateValidHotelParam.getPlatform());

        CommonResult commonResult = hotelInfoService.validHotel(updateValidHotelParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class),
                false);

        if(commonResult.getCode() == CommonResult.SUCCESS){
            List<String> sourceIds = Lists.newArrayList();
            sourceIds.add(String.valueOf(updateValidHotelParam.getId()));
            //更新酒店状态，发送Mq更新ES
            producerService.sendEsUpdate(new EsUpdateMessage().setType("hotel")
                    .setSourceType("yyks").setSourceIds(sourceIds),0);
        }
        return commonResult;
    }



    @ApiOperation("获取酒店类型")
    @PostMapping("/get/hotel/type")
    public CommonResult<List<AppHotelTypeDTO>> getHotelType(@RequestParam(value = "token", required = true) String token,
                                                            @RequestParam(value = "version", required = false) String version,
                                                            @RequestParam(value = "platform", required = false) String platform){
        AppAccountDTO appAccountDTO = validToken(token, version, platform);

        return hotelInfoService.getHotelType(MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }

    @ApiOperation("获取酒店星级")
    @PostMapping("/get/star/level")
    public CommonResult<List<AppStarLevelDTO>> getStarLevel(@RequestParam(value = "token", required = true) String token,
                                                            @RequestParam(value = "version", required = false) String version,
                                                            @RequestParam(value = "platform", required = false) String platform){
        AppAccountDTO appAccountDTO = validToken(token, version, platform);

        return hotelInfoService.getStarLevel(MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }


    /**
     * 修改酒店的入住须知(这个信息来自于 酒店的extra_info中)
     */
    @AdminAppLog(value = HOTEL_UPDATE_EXTRA)
    @ApiOperation("编辑入住须知")
    @PostMapping("/edit/note")
    public CommonResult editRoomNote(@Valid SetRoomNoteParam setRoomNoteParam){

        AppAccountDTO appAccountDTO = validToken(setRoomNoteParam.getToken(), setRoomNoteParam.getVersion(), setRoomNoteParam.getPlatform());

        return hotelInfoService.updateRoomNote(setRoomNoteParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));

    }

    @ApiOperation("获取酒店入住须知")
    @PostMapping("/get/note")
    public CommonResult<AppRoomNoteDTO> getRoomNote(@RequestParam(value = "token",required = true)String token,
                                                    @RequestParam(value = "version", required = false) String version,
                                                    @RequestParam(value = "platform", required = false) String platform){

        AppAccountDTO appAccountDTO = validToken(token, version, platform);

        return hotelInfoService.getRoomNote(MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }

}
